<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2018/6/12
 * Time: 16:02
 */

namespace App\Repository;


use App\Order;
use Illuminate\Support\Facades\Storage;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\TemplateProcessor;

class WordRepository
{
    public function word(Order $order)
    {
        Settings::setZipClass(Settings::PCLZIP);
        $word = new PhpWord();

        $word->setDefaultFontSize(12);
        $word->setDefaultFontName('方正仿宋简体');

        $section = $word->addSection();
        $section->addText('                    HSE联系点查出问题整改关闭一览表', ['size' => 18, 'alignment' => 'center']);
        $section->addText('检查时间:' .$order->begin_at . '                                         ' . '关闭时间:' . $order->end_at, ['size' => 14]);

        $tableStyle = ['borderSize' => 6, 'borderColor' => '000000'];
        $tableRowStyle = ['borderBottomColor' => '0000FF'];

        $table = $section->addTable($tableStyle, $tableRowStyle);
        $cell = ['valign' => 'center', 'borderColor' => '006699'];

        $row = $table->addRow(50, $cell);

        $row->addCell(2500)->addText('检查人');
        $row->addCell(2500)->addText('受检单位负责人', $cell);
        $row->addCell(2500)->addText('隐患点负责人', $cell);
        $row->addCell(2500)->addText('整改负责人', $cell);

        $row = $table->addRow();
        $sign = ['height' => 40, 'width' => 100];
        $cell = $row->addCell(2500, ['vMerge' => 'restart']);
        if ($order->inspector) {
            $cell->addImage('./' . $order->inspector, $sign );
        }

        $cell = $row->addCell(2500, ['vMerge' => 'restart']);
        if ($order->unit) {
            $cell->addImage('./' . $order->unit, $sign);
        }

        $cell = $row->addCell(2500, ['vMerge' => 'restart']);
        if ($order->point) {
            $cell->addImage('./' . $order->point, $sign );
        }

        $cell = $row->addCell(2500, ['vMerge' => 'restart']);
        if ($order->rectify) {
            $cell->addImage('./' . $order->rectify, $sign );
        }

        $row = $table->addRow();
        $row->addCell(null, ['vMerge' => 'restart'])->addText('受检单位');
        $row->addCell(null, ['vMerge' => 'restart', 'gridSpan' => 3])->addText($order->company);

        $row = $table->addRow();
        $row->addCell()->addText('受检班组');
        $row->addCell(null, ['vMerge' => 'restart', 'gridSpan' => 3])->addText($order->team);

        $row = $table->addRow();
        $row->addCell()->addText('查出问题描述');
        $row->addCell(null, ['vMerge' => 'restart', 'gridSpan' => 3])->addText($order->description);

        $font10 = ['size' => 8];
        $imgList = ['width' => 200];
        if ($order->law) {
            $row = $table->addRow(null, ['vMerge' => 'restart']);

            $row->addCell(500, ['vMerge' => 'restart'])->addText('不合规依据');
            $row->addCell(7500, ['vMerge' => 'restart', 'gridSpan' => 3])->addText($order->law, $font10);
        }



        if ($order->system) {
            $row = $table->addRow();
            $row->addCell(500, ['vMerge' => 'continue']);
            $row->addCell(7500, ['vMerge' => 'restart', 'gridSpan' => 3])->addText($order->system, $font10);
        }

        if ($order->grade) {
            $row = $table->addRow();
            $row->addCell(500, ['vMerge' => 'continue']);
            $row->addCell(7500, ['vMerge' => 'restart', 'gridSpan' => 3])->addText($order->grade, $font10);
        }

        if ($order->peccancy) {
            $row = $table->addRow();
            $row->addCell(500, ['vMerge' => 'continue']);
            $row->addCell(7500, ['vMerge' => 'restart', 'gridSpan' => 3])->addText($order->peccancy, $font10);
        }

        $row = $table->addRow();
        $row->addCell(2500, ['vMerge' => 'restart'])->addText('内审类别');
        $row->addCell(7500, ['vMerge' => 'restart', 'gridSpan' => 3])->addText($order->audit, $font10);

        $row = $table->addRow();
        $row->addCell(2500, ['vMerge' => 'restart'])->addText('整改措施建议参考');
        $row->addCell(7500, ['vMerge' => 'restart', 'gridSpan' => 3])->addText($order->proposal, $font10);

        $row = $table->addRow();
        $row->addCell(5000, ['gridSpan' => 2])->addText('问题图片');
        $row->addCell(5000, ['gridSpan' => 2])->addText('整改图片');


        $row = $table->addRow();

        $cell = $row->addCell(5000, ['gridSpan' => 2]);
        if ($order->problem) {
            $cell->addImage('./'. $order->problem, $imgList);
        }
        $cell = $row->addCell(5000, ['gridSpan' => 2]);
        if ($order->rectification) {
            $cell->addImage('./'. $order->rectification, $imgList);
        }



        $write = IOFactory::createWriter($word);
        $name = 'word/'.$order->company. ' - ' . $order->team . '- HES联系点问题关闭.docx';
        $write->save('./' . $name);
        return $name;
    }

    public function loadTemp(Order $order)
    {
        Settings::setZipClass(Settings::PCLZIP);
        $temp = new TemplateProcessor('./temp.docx');

        $temp->setValue('begin_at', $order->begin_at);
        $temp->setValue('end_at', $order->end_at);

        $temp->saveAs('./1234.docx');

    }

    public static function base64Img($file)
    {
        $img_info = mime_content_type($file);
        $fp = fopen($file, 'rb', filesize($file));
        $img_data = fread($fp, filesize($file));
        $base64_img = 'data:' . $img_info . ';base64,' .chunk_split(base64_encode($img_data));
        fclose($fp);

        return $base64_img;
    }
}